#include <iostream>
#include "domains/IAxisAlignedDomain.hpp"
#include "loaders/AxisAlignedLoader.hpp"
#include "loaders/UniformAxisAlignedLoader.hpp"
#include "other/utils.hpp"

UniformAxisAlignedLoader::UniformAxisAlignedLoader(IAxisAlignedDistribution *_sdist, IAxisAlignedDistribution *_vdist, double _density, double _np2c) : AxisAlignedLoader(_sdist,_vdist) {
	density=_density;
	np2c=_np2c;
}

UniformAxisAlignedLoader::~UniformAxisAlignedLoader() {
}

void UniformAxisAlignedLoader::setDomain(IAxisAlignedDomain *_dom) {
	AxisAlignedLoader::setDomain(_dom);
//	std::cout << xmin << "," << xmax << " " << ymin << "," << ymax << std::endl;
}

void UniformAxisAlignedLoader::load(IParticlePopulation *pop) {
	unsigned nptcl=(xmax-xmin)*(ymax-ymin)*density/np2c;
//	std::cout << "nptcl=" << nptcl << std::endl;
	//
	double x,y,u,v;
	Vector pos(x,y);
	//
	unsigned cnt=0;
	while (cnt<nptcl) {
//		std::cout << "cnt=" << cnt << std::endl;
//		std::cout << dom->get_axis1_min() << "," << dom->get_axis1_max() << "; " << dom->get_axis2_min() << "," << dom->get_axis2_max() << std::endl;
		do {
			sdist->sample(x,y,randunity(),randunity());	// get first spatial sampling to enable first domain comparison in loop
			pos.x=x;
			pos.y=y;
		} while (!dom->contains(pos));
		vdist->sample(u,v,randunity(),randunity());
		//
		Particle *p=new Particle();
		p->x=x;
		p->y=y;
		p->z=0;
		p->u=u;
		p->v=v;
		p->w=0;
//		std::cout << p->x << " " << p->y << std::endl;
		pop->add(p);
		//
		cnt++;
	}
}